package com.qrlib.decode.impl;

import android.graphics.Rect;
import android.support.annotation.NonNull;
import com.qrlib.decode.DecodeCallback;
import com.qrlib.decode.DecodeWorker;
import com.qrlib.utils.LogUtils;

/**
 * 微信提供的 QbarWorker 封装
 *
 * 注意：QbarNative 同一进程只能存在一个实例！目前用 activity 限制
 *
 * (非线程安全) Created by zhengxiao on 09/08/2017.
 */

public class QbarWorker implements DecodeWorker {


  private final DecodeCallback callback;
  private boolean running = false;
  private QbarDaemon qbarDaemon;

  public QbarWorker(DecodeCallback callback) {
    this.callback = callback;
  }

  @Override
  public boolean isRunning() {
    return running;
  }

  @Override
  public synchronized void start() {
    LogUtils.d("start: ");
    running = true;
    qbarDaemon = QbarDaemon.getInstance();
  }

  @Override
  public synchronized void stop() {
    LogUtils.d("stop: ");
    running = false;
    qbarDaemon = null;
  }


  @Override
  public synchronized void decode(@NonNull byte[] data, int width, int height, boolean rotate,
      Rect crop) {
    if (qbarDaemon != null) {
      qbarDaemon.postRequest(new ScanFrameRequest(data, width, height, rotate, crop,
          callback));
    }
  }
}
